package Year21_Month12.day1213;

public class Solution {
    //山脉数组的峰顶索引
    public int peakIndexInMountainArray1(int[] arr) {
        //首先第一种：直接遍历
        //可以从一头开始遍历，也可以从两头开始遍历
        int left = 0;
        while(left < arr.length){
            if(arr[left + 1] < arr[left]){
                return left;
            }
            left++;
        }
        return -1;
    }

    //第二种：二分查找
    public static int peakIndexInMountainArray2(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int mid = (left + right) >> 1;
            if(arr[mid - 1] < arr[mid] && arr[mid] < arr[mid + 1]){
                left = mid;
            }else if(arr[mid - 1] > arr[mid] && arr[mid] > arr[mid + 1]){
                right = mid;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {24,69,100,99,79,78,67,36,26,19};
        int index = peakIndexInMountainArray2(arr);
        System.out.println(index);
    }
}
